package com.example.github.reflect.demo;

import com.example.github.reflect.entity.Person;

import java.lang.reflect.Method;

public class ReflectMethod {
    public static void main(String[] args) throws Exception {
        Class<Person> personClass = Person.class;
        // 拿到有参数的方法
        Method method = personClass.getMethod("eat", String.class);
        Person person = new Person();
        // 调用指定对象
        method.invoke(person, "饭饭");
        System.out.println("=======================");
        // 拿到无参方法
        Method eat = personClass.getMethod("eat");
        eat.invoke(person);
        System.out.println("=======================");
        // 获取类中的所有方法
        Method[] methods = personClass.getMethods();
        for (Method m : methods) {
            System.out.println(m);
        }
    }
}
